package com.java.Filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/filter1/*") // 上下文为/filter1的请求都先经过该过滤器
public class Filter1 implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;character=utf-8");

        System.out.println("过滤器1过滤前");
        // doFilter：必须调用此方法，否则程序不会继续向下执行
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("过滤器1过滤后");
    }
}
